//
// CrackPluginKnob
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crack.plugindev;

import java.io.*;
import net.jextra.crack.*;
import net.jextra.crack.tool.*;

/**
 * <p>
 * This is a plugin to develop Crack Plugins. It knows how to install the plugin
 * directly into the Crack installation. It also knows how to publish the plugin to a
 * Crack Updater.
 * </p>
 */
public class CrackPluginKnob
{
    // ============================================================
    // Fields
    // ============================================================

    private CrackPluginKnobParams params;

    // ============================================================
    // Constructors
    // ============================================================

    public CrackPluginKnob( CrackPluginKnobParams params )
    {
        this.params = params;
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    @CrackCommand( description = "Install this plugin into local Crack application." )
    public void install()
        throws Exception
    {
        assert params.getBuildName() != null : "PluginName can not be null.";
        assert params.getBuildDir() != null : "BuildDir can not be null.";

        CrackBuild.getInstance().invokeCommand( "build" );

        File libDir = CrackBuild.getInstance().getCrackContext().getLibDir();
        libDir.mkdirs();

        FileCopier copier = new FileCopier();
        copier.copyDir( params.getBuildDir(), libDir );
    }

    @CrackCommand( description = "Publish this plugin to the Crack Updater/s." )
    public void publish()
        throws Exception
    {
        assert params.getBuildName() != null : "PluginName can not be null.";
        assert params.getBuildDir() != null : "BuildDir can not be null.";

        CrackBuild.getInstance().invokeCommand( "build" );

        for ( File file : params.getBuildDir().listFiles() )
        {
            System.out.println( "uploading file " + file.getCanonicalPath() +
                " to crack updater." );
            CrackBuild.getInstance().getCrackContext().getUpdater().uploadFile( "lib/" +
                file.getName(), file );
        }
    }
}

